Ext.define('App.projectorder.view.ProjectOrderTreeGrid', {
    extend: 'App.projectorder.view.BaseProjectOrderTree',
    alias: 'widget.ProjectOrderTreeGrid',
    config: {
        tbar: {
            dcLog: {
                text: '采集日志',
                handler: function () {
                    Ext.create('App.datacollection.dclog.view.DcLogDataWindow').show();
                }
            },
            collectPO: {
                text: 'PO采集',
                handler: function () {
                    var me = this.up('ProjectOrderTreeGrid');
                    me.collectPo();
                }
            },
            sync: {
                text: '同步',
                disabled: true,
                handler: function () {
                    var me = this.up('ProjectOrderTreeGrid');
                    var records = me.getSelection();
                    if (records.length > 0) {
                        me.sync(records[0]);
                    }
                }
            },
            add: {
                text: '导入未关联',
                iconCls: 'add',
                disabled: true,
                handler: function () {
                    var me = this.up('ProjectOrderTreeGrid');
                    var selection = me.getChecked();
                    if (selection.length > 0) {
                        Ext.create('Extend.window.SelectionWindow', {
                            title: '选择未关联PO',
                            height: 500,
                            grid: Ext.create('App.projectorder.view.NotRefProjectOrderGrid', {
                                forceFit: false,
                                bind: {
                                    store: '{NotRefQuery}'
                                }
                            }),
                            listeners: {
                                selected: function (rs) {
                                    if (rs.length > 0) {
                                        me.importProjectOrder(selection[0], rs);
                                    }
                                }
                            }
                        }).show();
                    }

                }
            },
            cancel: {
                text: '取消关联',
                iconCls: 'remove',
                disabled: true,
                handler: function () {
                    var view = this.up('ProjectOrderTreeGrid');
                    var selection = view.getChecked();
                    if (selection.length > 0) {
                        view.cancelProjectOrderToOrganization(selection[0]);
                    }
                }
            },
            refresh: {
                text: '刷新',
                handler: function () {
                    var view = this.up('ProjectOrderTreeGrid');
                    var selection = view.getChecked();
                    if (selection.length > 0) {
                        view.reloadNode(selection[0]);
                    } else {
                        var root = view.getRootNode();
                        root.set('loaded', false);
                        root.set('expanded', false);
                        view.expandNode(root, false);
                    }
                }
            }
        }
    },
    columns: [{
        xtype: 'treecolumn',
        text: '名称',
        dataIndex: 'name'
    }, {
        text: '类型',
        dataIndex: 'type'
    }, {
        text: '创建人',
        dataIndex: 'creator'
    }, {
        xtype: 'cdtcolumn',
        text: '创建时间',
        dataIndex: 'cdt'
    }],
    listeners: {
        itemdblclick: function ($this, record, item, index, e, eOpts) {
            if (record.get('type').toLowerCase() == 'po' || record.get('type').toLowerCase() == 'project') {
                this.editHandler.call(this, this, null, record);
            }
        }
    }
    ,
    initComponent: function () {
        var me = this;
        this.callParent(arguments);
        me.on({
            selectionchange: function ($this, selected, eOpts) {
                var me = this;
                me.down('button[text="同步"]').disable();
                me.down('button[text="导入未关联"]').disable();
                me.down('button[text="取消关联"]').disable();
                if (selected.length > 0) {
                    if (selected[0].get('type').toLowerCase() == 'po' || selected[0].get('type').toLowerCase() == 'project') {
                        me.down('button[text="同步"]').enable();
                    } else if (selected[0].get('type').toLowerCase() == 'pdu') {
                        me.down('button[text="导入未关联"]').enable();
                    }
                    if (selected[0].get('type').toLowerCase() == 'po') {
                        me.down('button[text="取消关联"]').enable();
                    }
                }
            }
        })
    }
    ,
    editHandler: function (me, parent, node) {
        if (node.get('type') == 'po') {
            this.editProjectOrder(me, parent, node)
        } else if (node.get('type') == 'project') {
            this.editProject(me, parent, node)
        }
    }
    ,
    editProjectOrder: function (me, parent, node) {
        var clazz = 'App.projectorder.view.ProjectOrderEditorWindow';
        var url = 'projectorder/save';
        if (!Ext.isEmpty(parent)) {
            var node = {
                organization: {
                    id: parent.get('id')
                }
            };
            Ext.create(clazz, {
                url: url,
                listeners: {
                    save: function () {
                        parent.set('loaded', false);
                        parent.set('expanded', false);
                        me.expandNode(parent, false);
                    }
                }
            }).show().loadRecord(node);
        } else {
            var getUrl = 'projectorder/edit';
            parent = node.parentNode;
            Ext.create(clazz, {
                url: url,
                model: 'App.projectorder.model.ProjectOrderModel',
                listeners: {
                    save: function () {
                        parent.set('loaded', false);
                        parent.set('expanded', false);
                        me.expandNode(parent, false);
                    }
                }
            }).show().load({
                url: getUrl,
                params: {
                    id: node.get('id')
                }
            });
        }
    }
    ,
    editProject: function (me, parent, node) {
        var clazz = 'App.project.view.ProjectBaseinfoEditorWindow';
        var url = 'project/udpate_baseinfo';
        if (!Ext.isEmpty(parent)) {
            var node = {
                projectOrder: {
                    id: parent.get('id'),
                    po: parent.get('po'),
                    name: parent.get('name')
                }
            }
            Ext.create(clazz, {
                url: url,
                listeners: {
                    save: function () {
                        parent.set('loaded', false)
                        parent.set('expanded', false);
                        me.expandNode(parent, false);
                    }
                }
            }).show().loadRecord(node);
        } else {
            var getUrl = 'project/edit';
            parent = node.parentNode;
            Ext.create(clazz, {
                url: url,
                model: 'App.project.model.ProjectModel',
                listeners: {
                    save: function () {
                        parent.set('loaded', false)
                        parent.set('expanded', false);
                        me.expandNode(parent, false);
                    }
                }
            }).show().load({
                url: getUrl,
                params: {
                    id: node.get('id')
                }
            });
        }
    }
    ,
    collectPo: function () {
        var me = this;
        me.mask('PO采集中，采集时间时间较长，请稍后......');
        Ext.Ajax.request({
            url: 'remote/project_order/collection',
            success: function (response, opts) {
                Ext.Msg.alert("提示", response.result.result);
                me.unmask();
            },
            failure: function (response, opts) {
                me.unmask();
            }
        });
    }
    ,
    sync: function (record) {
        var me = this;
        me.mask('同步......');
        Ext.Ajax.request({
            url: 'remote/project_order/sync',
            params: {type: record.get('type'), id: record.get('type') == 'po' ? record.get('po') : record.get('projectCode')},
            success: function (response, opts) {
                if (response.result.success == true) {
                    Ext.Msg.alert("提示", response.result.result);
                }
                me.unmask();
            },
            failure: function (response, opts) {
                me.unmask();
            }
        });
    }
    ,
    importProjectOrder: function (parent, rs) {
        var ids = [];
        var me = this;
        Ext.Array.each(rs, function (item) {
            ids.push(item.get('id'));
        });
        me.mask('处理中...');
        Ext.Ajax.request({
            url: 'projectorder/association_organization',
            params: {'organizationId': parent.get('id'), 'ids': ids},
            success: function (response, opts) {
                Ext.Msg.alert("提示", response.result.result);
                me.reloadNode(parent);
            },
            callback: function (response, opts) {
                me.unmask()
            }
        });
    },
    cancelProjectOrderToOrganization: function (parent) {
        var me = this;
        me.mask('处理中...');
        Ext.Ajax.request({
            url: 'projectorder/cancellation_association_organization',
            params: {'id': parent.get('id')},
            success: function (response, opts) {
                Ext.Msg.alert("提示", response.result.result);
                me.reloadNode(parent.parentNode);
            },
            callback: function (response, opts) {
                me.unmask()
            }
        });

    }

})
;